package com.penjin.android.receiver;

import android.content.Context;
import android.util.Log;

import com.nostra13.universalimageloader.utils.L;
import com.penjin.android.domain.PenjinStaff;
import com.penjin.android.domain.PenjinUser;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by lxl on 2016/3/11.
 */
public class JPushSetting {

    private static String TAG = "JPush" ;

    public static void setAliasAndTags(Context context , PenjinUser penjinUser , PenjinStaff penjinStaff ){

        Set<String> tags = new HashSet<String>() ;

        tags.add(penjinUser.getRegion()) ;
        tags.add(penjinUser.getCompanyId());
        tags.add(penjinUser.getSex()) ;


        tags.add(penjinStaff.getDepartment()) ;

        String sex = "男" ;

        setAliasAndTags(context,penjinUser.getPhone(),tags);

    }

    public static void setAliasAndTags( Context context ,
                                        String telephone ,
                                        Set<String> tags
    ){

        Log.d(TAG,"setAliasAndTags") ;

        if( tags == null ){

            Log.d(TAG,"tags is null") ;
            return;

        }else{

            tags = JPushInterface.filterValidTags(tags) ;
            if( tags.isEmpty() ){

                Log.d(TAG,"tags is null") ;
                return;

            }

        }
        if( telephone == null ){

            Log.d(TAG,"telephone is null") ;
            return;

        }

        JPushInterface.setAliasAndTags(context,telephone,tags,new JpushCallback());

    }

    public static void setAlias( Context context ,
                                    String telephone ){

        Log.d(TAG,"setAlias") ;

        if( telephone == null || telephone.isEmpty() ){

            Log.d(TAG,"telephone is null") ;
            return;

        }
        JPushInterface.setAlias(context,telephone,new JpushCallback());

    }

    public static void setTags( Context context ,
                                Set<String> tags ){

        Log.d(TAG,"setTags") ;
        if( tags == null ){

            Log.d(TAG,"tags is null") ;
            return;

        }else{

            tags = JPushInterface.filterValidTags(tags) ;
            if( tags.isEmpty() ){

                Log.d(TAG,"tags is null") ;
                return;

            }

        }
        JPushInterface.setTags(context,tags,new JpushCallback());

    }

    private static class JpushCallback implements TagAliasCallback{

        @Override
        public void gotResult(int responseCode, String alias, Set<String> tags ) {

            Log.d(TAG,"alias=" + alias) ;

            if(tags != null ){

                Log.d(TAG,"tags=" + tags.toString()) ;

            }


            String logs ;
            switch (responseCode) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.i(TAG, logs);
                    break;

                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.i(TAG, logs);
                    break;

                default:
                    logs = "Failed with errorCode = " + responseCode;
                    Log.e(TAG, logs);
            }

        }
    }

}
